package com.tanhua.recommend.listener;

import com.tanhua.mongo.PublishScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@RocketMQMessageListener(topic = "tanhua-bigdata",consumerGroup = "tanhua-bigdata-group")
@Component
public class bigDataListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map map) {
        System.out.println("开始收集插入用户行为....");
        Integer userId = Integer.parseInt(map.get("userId").toString());
        Integer pid = Integer.parseInt(map.get("pid").toString());
        //行为  1：浏览  2：点赞  3：喜欢 4：评论  5：取消点赞  6：取消喜欢
        Integer type = Integer.parseInt(map.get("type").toString());

        Double score = 0D;
        switch (type){
            case 1:{
                score = 1D;
                break;
            }case 2:{
                score = 5D;
                break;
            }case 3:{
                score = 8D;
                break;
            }case 4:{
                score = 10D;
                break;
            }case 5:{
                score = -5D;
                break;
            }case 6:{
                score = -8D;
                break;
            }default:{
                break;
            }
        }

        PublishScore publishScore = new PublishScore();
        publishScore.setUserId(userId.longValue());
        publishScore.setDate(System.currentTimeMillis());
        publishScore.setPublishId(pid.longValue());
        publishScore.setScore(score);
        publishScore.setDate(new Date().getTime());

        mongoTemplate.insert(publishScore);

    }
}
